import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/user/User.vue'
import Permission from '../components/power/rights.vue'
import Role from '../components/power/role.vue'
import Goods from '../components/goods/goods.vue'
import Tag from '../components/goods/tag.vue'
import Order from '../components/order/order.vue'
import Theme from '../components/postmain/theme.vue'
import PostMain from '../components/postmain/postmain.vue'
import Reply from '../components/postmain/reply.vue'
import EGame from '../components/egame/EGame.vue'
import Main from '../components/egame/Main.vue'
import Search from '../components/egame/Search.vue'
import ShopCar from '../components/egame/ShopCar.vue'
import MyInfo from '../components/egame/MyInfo.vue'
import GoodsInfo from '../components/egame/goods/GoodsInfo.vue'
import Add from '../components/goods/Add.vue'
import UserLogin from '../components/egame/login/UserLogin.vue'
import OrderInfo from '../components/egame/OrderInfo.vue'
import Pay from '../components/egame/Pay.vue'
import UserPostMain from '../components/egame/postmain/PostMain.vue'
import MyReply from '../components/egame/postmain/MyReply.vue'
import AddPostMain from '../components/egame/postmain/AddPostMain.vue'
import MyOrder from '../components/egame/MyOrder.vue'
import OrderDetail from '../components/egame/OrderDetail.vue'
import Register from '../components/egame/login/Register.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: 'userlogin'},
  { path: '/login', component: Login },
  { 
    path: '/home',
    component: Home, 
    redirect: '/welcome',
    children: [
    { path: '/welcome', component: Welcome},
    { path: '/user', component: User},
    { path: '/permission', component: Permission},
    { path: '/role', component: Role},
    { path: '/goods', component: Goods},
    { path: '/tag', component: Tag},
    { path: '/order', component: Order},
    { path: '/theme', component: Theme},
    { path: '/post', component: PostMain},
    { path: '/reply', component: Reply},
    { path: '/goods/add', component: Add}
  ]},
  { path: '/egame', 
    component: EGame,
    redirect: '/main',
    children: [
      { path: '/main', component: Main },
      { path: '/search', component: Search},
      { path: '/shopcar', component: ShopCar},
      { path: '/myinfo', component: MyInfo},
      { path: '/goodsInfo/', name: 'goodsInfo', component: GoodsInfo},
      { path: '/orderInfo' , name: 'orderInfo', component: OrderInfo},
      { path: '/pay', name: 'pay', component: Pay},
      { path: '/userPostMain', name: 'userPostMain', component: UserPostMain},
      { path: '/myReply', component: MyReply},
      { path: '/addPostMain', component: AddPostMain},
      { path: '/myOrder', component: MyOrder},
      { path: '/orderDetail', name: 'orderDetail', component: OrderDetail}
    ]
  },
  { path: '/userlogin' ,component: UserLogin},
  { path: '/register' ,component: Register}
]

const router = new VueRouter({
  routes
})
// 挂载路由守卫
router.beforeEach((to, from, next) => {
  //to将要访问的路径
  //from 代表从哪个路径而来
  //next是一个函数，代表放行
  // newt ()放行 next('/login') 强制跳转
  if (to.path === '/login') return next()
  if (to.path === '/userlogin') return next()
  if (to.path === '/register') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/userlogin')
  next()
})

export default router
